import request from '@/utils/request'
import config from '@/config'
import { userStorage } from '@/utils/storage'

// 首页接口
export const homeApi = {
    // 获取民宿信息
    getHotelInfo: (id) => request({
        url: `/hotel/hotel/${id}`
    }),
    // 获取轮播图
    getBannerList: (data) => request({
        url: '/hotel/slider/all',
        method: 'GET',
        data
    })
}

// 点单页接口
export const orderApi = {
    // 获取商品分类
    getCategoryList: (data) => request({
        url: '/hotel/category/all',
        method: 'GET',
        data
    }),
    // 获取商品列表
    getGoodsList: (data) => request({
        url: '/hotel/good/all',
        method: 'GET',
        data
    }),
    // 保存订单
    saveOrder: (data) => request({
        url: '/hotel/order',
        method: 'POST',
        data
    }),
    // 获取订单列表
    getOrderList: (data) => request({
        url: '/hotel/order/list',
        method: 'GET',
        data
    }),
    // 取消订单
    cancelOrder: (data) => request({
        url: '/hotel/order',
        method: 'PUT',
        data
    })
}

// 预约接送接口
export const bookingApi = {
    // 提交预约
    submitBooking: (data) => request({
        url: '/hotel/booking',
        method: 'POST',
        data
    }),
    // 修改预约
    updateBooking: (data) => request({
        url: '/hotel/booking',
        method: 'PUT',
        data
    }),
    // 获取预约列表
    getBookingList: (data) => request({
        url: '/hotel/booking/list',
        method: 'GET',
        data
    }),
    // 删除预约
    deleteBooking: (data) => request({
        url: '/hotel/booking',
        method: 'PUT',
        data
    }),
    //获取车站列表
    getStationList: (data) => request({
        url: '/hotel/station/all',
        method: 'GET',
        data
    })
}

// 我的页面接口
export const mineApi = {
    // 获取用户信息
    getUserInfo: () => request({
        url: '/user/info'
    }),
    // 获取常见问题列表
    getQuestionList: () => request({
        url: '/hotel/question/all',
        method: 'GET',
    })  
}

// 用户相关接口
export const userApi = {
    // 微信登录（不带手机号）
    wxLogin: (data) => request({
        url: '/hotel/wx/login',
        method: 'POST',
        data
    }),
    
    // 微信手机号登录
    wxPhoneLogin: (data) => request({
        url: '/hotel/wx/login',
        method: 'POST',
        data
    }),
    
    // 更新用户信息
    updateUserInfo: (data) => request({
        url: '/hotel/user',
        method: 'PUT',
        data
    }),
	uploadAvatar: (data) => request({
	    url: '/hotel/user/uploadAvatar',
	    method: 'POST',
	    data
	})
}

// 文件上传接口
export const uploadApi = {
    // 上传文件
    uploadFile: (filePath) => {
        return new Promise((resolve, reject) => {
            uni.uploadFile({
                url: config.baseUrl + '/common/upload',
                filePath: filePath,
                name: 'file',
                header: {
                    'Authorization': `Bearer ${userStorage.getToken()}`
                },
                success: (uploadRes) => {
                    const res = JSON.parse(uploadRes.data)
                    if (res.code === 200) {
                        resolve(res.data)
                    } else {
                        reject(new Error(res.msg || '上传失败'))
                    }
                },
                fail: (err) => {
                    reject(err)
                }
            })
        })
    }
} 


